package com.business.system.service.impl;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.business.base.tools.key.PKConfContext;
import com.business.base.tools.key.PKIDTools;
import com.business.system.dao.TabCarOwnerInfoMapper;
import com.business.system.pojo.TabCarOwnerInfo;
import com.business.system.pojo.TabCarOwnerInfoExample;
import com.business.system.service.ICarOwnerInfoService;
import com.utility.Page;

@Service
public class CarOwnerInfoServiceImpl implements ICarOwnerInfoService {
	@Autowired
	TabCarOwnerInfoMapper carOwnerInfoMapper;

	@Override
	public int insert(TabCarOwnerInfo entity) throws RuntimeException {
		// TODO Auto-generated method stub
		entity.setCreateTime(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss")
				.format(new Date()));
		entity.setId(PKIDTools.getSerialID(PKConfContext.NO_Len_Four,
				PKConfContext.CarOwnerInfoID_DataPath,
				PKConfContext.CarOwnerInfoID_Suffix));
		return this.carOwnerInfoMapper.insert(entity);
	}

	@Override
	public int update(TabCarOwnerInfo entity) throws RuntimeException {
		// TODO Auto-generated method stub
		return this.carOwnerInfoMapper.updateByPrimaryKey(entity);
	}

	@Override
	public int delete(String id) throws RuntimeException {
		// TODO Auto-generated method stub
		return this.carOwnerInfoMapper.deleteByPrimaryKey(id);
	}

	@Override
	public TabCarOwnerInfo findById(String id) throws RuntimeException {
		// TODO Auto-generated method stub
		return this.carOwnerInfoMapper.selectByPrimaryKey(id);
	}

	@Override
	public List<TabCarOwnerInfo> findList(TabCarOwnerInfo entity)
			throws RuntimeException {
		// TODO Auto-generated method stub

		TabCarOwnerInfoExample example = new TabCarOwnerInfoExample();
		example.createCriteria().andCarTagNumberEqualTo(
				entity.getCarTagNumber());
		if (entity.getCarBrands() != null) {
			example.createCriteria().andCarBrandsLike(entity.getCarBrands());
		}
		if (entity.getCarEngineNo() != null) {
			example.createCriteria()
					.andCarEngineNoLike(entity.getCarEngineNo());
		}
		if (entity.getOwnerName() != null) {
			example.createCriteria().andOwnerNameLike(entity.getOwnerName());
		}
		return this.carOwnerInfoMapper.selectByExample(example);
	}

	@Override
	public Page<TabCarOwnerInfo> findListPage(TabCarOwnerInfo entity,
			int pageNo, int pageSize) throws RuntimeException {
		// TODO Auto-generated method stub
		Page<TabCarOwnerInfo> page = new Page<TabCarOwnerInfo>();
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("car_tag_number", entity.getCarTagNumber());
		map.put("car_brands", entity.getCarBrands());
		map.put("car_engine_no", entity.getCarEngineNo());
		map.put("owner_name", entity.getOwnerName());
		page.setParams(map);
		page.setPageNo(pageNo);
		page.setPageSize(pageSize);

		List<TabCarOwnerInfo> list = this.carOwnerInfoMapper.findListPage(page);
		page.setResults(list);
		return page;
	}

}
